import * as echarts from '../../ec-canvas/echarts';

const app = getApp();
var ec = {}
var chart
var chart1
var chart2

function initChart(canvas, width, height, dpr) {
    console.log(canvas, width, height, dpr, "11111111&&&&&&&&&&")
    let startdate = wx.getStorageSync('startdate')
    let enddate = wx.getStorageSync('enddate')
    let type = wx.getStorageSync('type')
    chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
    });
    canvas.setChart(chart);
    ec[canvas.canvasId] = chart

    let that = this;
    let data = {
        "userId": 18721260590,
        "startDate": startdate,
        "endDate": enddate,
        "type": 0
    }
    wx.request({
        url: 'https://calamar.top:8081/measure/history',
        method: 'POST',
        data: data,    //参数为键值对字符串
        header: {
            //设置参数内容类型为x-www-form-urlencoded
            'content-type': 'application/json'
        },
        success: function (res) {
            console.log(res.data)
            let data = [];
            let dateData = [];
            for (let i in res.data.data) {
                data.push(res.data.data[i].weight)
                dateData.push(res.data.data[i].createTime)
            }
            var option = {
                title: {
                    left: 'center'
                },
                // legend: {
                //   data: ['A', 'B', 'C'],
                //   top: 50,
                //   left: 'center',
                //   backgroundColor: 'red',
                //   z: 100
                // },
                grid: {
                    containLabel: true
                },
                tooltip: {
                    show: true,
                    trigger: 'item'
                },
                xAxis: {
                    type: 'category',
                    interval: 0,//使x轴横坐标全部显示
                    boundaryGap: false,
                    data: dateData
                    //data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
                    // show: false
                },
                yAxis: {
                    name: '体重(斤)',
                    show: true,
                    x: 'center',
                    type: 'value',
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    }
                },
                series: [{
                    name: '体重',
                    type: 'line',
                    smooth: true,
                    data: data
                }]
            };

            chart.setOption(option);

        }
    })
    return chart;
}

function initChart1(canvas, width, height, dpr) {
    console.log(canvas, width, height, dpr, "11111111&&&&&&&&&&")
    let startdate = wx.getStorageSync('startdate')
    let enddate = wx.getStorageSync('enddate')
    let type = wx.getStorageSync('type')
    chart1 = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
    });
    canvas.setChart(chart1);

    let that = this;
    let data = {
        "userId": 18721260590,
        "startDate": startdate,
        "endDate": enddate,
        "type": 0
    }
    wx.request({
        url: 'https://calamar.top:8081/measure/history',
        method: 'POST',
        data: data,    //参数为键值对字符串
        header: {
            //设置参数内容类型为x-www-form-urlencoded
            'content-type': 'application/json'
        },
        success: function (res) {
            console.log(res.data)
            let data = [];
            let dateData = [];
            for (let i in res.data.data) {
                data.push(res.data.data[i].hp)
                dateData.push(res.data.data[i].createTime)
            }
            var option = {
                title: {
                    left: 'center'
                },
                // legend: {
                //   data: ['A', 'B', 'C'],
                //   top: 50,
                //   left: 'center',
                //   backgroundColor: 'red',
                //   z: 100
                // },
                grid: {
                    containLabel: true
                },
                tooltip: {
                    show: true,
                    trigger: 'item'
                },
                xAxis: {
                    type: 'category',
                    interval: 0,//使x轴横坐标全部显示
                    boundaryGap: false,
                    data: dateData
                    // data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],

                },
                yAxis: {
                    name: '心跳（次/分钟）',
                    x: 'center',
                    show: true,
                    type: 'value',
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    }
                },
                series: [{
                    name: '心跳',
                    type: 'line',
                    smooth: true,
                    data: data
                }]
            };

            chart1.setOption(option);

        }
    })
    return chart1;
}

function initChart2(canvas, width, height, dpr) {
    console.log(canvas, width, height, dpr, "11111111&&&&&&&&&&")
    let startdate = wx.getStorageSync('startdate')
    let enddate = wx.getStorageSync('enddate')
    let type = wx.getStorageSync('type')
    chart2 = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
    });
    canvas.setChart(chart);

    let that = this
    let data = {
        "userId": 18721260590,
        "startDate": startdate,
        "endDate": enddate,
        "type": 0
    }
    wx.request({
        url: 'https://calamar.top:8081/measure/history',
        method: 'POST',
        data: data,   //参数为键值对字符串
        header: {
            //设置参数内容类型为x-www-form-urlencoded
            'content-type': 'application/json'
        },
        success: function (res) {
            console.log(res.data.data)
            let data = [];
            let data1 = [];
            let dateData = [];
            for (let i in res.data.data) {
                data.push(res.data.data[i].lowBp)
                data1.push(res.data.data[i].highBp)
                dateData.push(res.data.data[i].createTime)
            }
            console.log('data', data)
            console.log('data1', data1)
            var option = {
                title: {
                    left: 'left'
                },
                // legend: {
                //   data: ['A', 'B', 'C'],
                //   top: 50,
                //   left: 'center',
                //   backgroundColor: 'red',
                //   z: 100
                // },
                grid: {
                    containLabel: true
                },
                tooltip: {
                    show: true,
                    trigger: 'item'
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,

                    data: dateData
                    //data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
                    // show: false

                },
                yAxis: {
                    name: '血压图（bp)',
                    x: 'center',
                    type: 'value',
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    }
                    // show: false
                },
                series: [
                    {
                        name: '高压',
                        type: 'line',
                        smooth: true,
                        data: data
                    }, {
                        name: '低压',
                        type: 'line',
                        smooth: true,
                        data: data1
                    }]
            };

            chart2.setOption(option);

        }
    })
    return chart2;
}


Page({

    data: {
        List1: [],
        type: 0,
        startdate: '',
        enddate: '',
        datalowBp: [],
        datahighBp: [],
        datahp: [],
        dataweight: [],
        datecreateTime: [],

        ec: {
            onInit: initChart
        },
        ec2: {
            onInit: initChart1
        },
        ec3: {
            onInit: initChart2
        }
    },

    StartDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            startdate: e.detail.value
        })
    },
    EndDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            enddate: e.detail.value
        })
    },
    navPage() {

        console.log('进来了'),

            wx.request({
                url: 'https://calamar.top:8081/measure/todayIsInput', //仅为示例，并非真实的口址
                method: 'post',
                data: {
                    userId: 18721260590
                },
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success(res) {
                    console.log('接口返回值', res.data.data.hp)
                    console.log('接口返回值', res.data.data.weight)
                    console.log('接口返回值', res.data.data.bp)
                    let bp = res.data.data.bp
                    let weight = res.data.data.weight
                    let hp = res.data.data.hp
                    let highBpData = res.data.data.highBpData
                    let lowBpData = res.data.data.lowBpData
                    let weightData = res.data.data.weightData
                    let hpData = res.data.data.hpData
                    console.log('bp', bp)
                    console.log('weight', weight)
                    console.log('hp', hp)
                    wx.navigateTo({
                        url: '../healthtest/addHealth?hp=' + hp + '&weight=' + weight + '&bp=' + bp + '&hpData='
                            + hpData + '&weightData=' + weightData + '&highBpData='
                            + highBpData + '&lowBpData=' + lowBpData,
                    })

                }
            })

    },

    type1() {

        this.setData({
            type: 0
        })
        this.search()
    },
    type2() {
        this.setData({
            type: 1
        })
        this.search()
    },
    type3() {
        this.setData({
            type: 2
        })
        this.search()
    },
    type4() {
        this.setData({
            type: null
        })
        this.search()
    },

    search() {
        console.log('this.startdate', this.data.startdate)
        console.log('this.enddate', this.data.enddate)
        console.log('this.type', this.data.type)
        var that = this
        that.setData({
            datalowBp: [],
            datahighBp: [],
            datahp: [],
            dataweight: [],
            datecreateTime: []
        })
        wx.request({
            url: 'https://calamar.top:8081/measure/history',
            method: 'POST',
            data: {
                "userId": 18721260590,
                // "startDate":this.data.startdate,
                // "endDate":this.data.enddate,
                "startDate": that.data.startdate,
                "endDate": that.data.enddate,
                "type": that.data.type
            },   //参数为键值对字符串
            header: {
                //设置参数内容类型为x-www-form-urlencoded
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log('this.startdate', that.data.startdate)
                console.log('this.enddate', that.data.enddate)
                console.log('this.type', that.data.type)
                // console.log('this.datalowBp',datalowBp)

                that.setData({
                    startdate: res.data.data[0].startDate,
                    enddate: res.data.data[0].endDate,
                })
                for (let i in res.data.data) {

                    that.data.datalowBp.push(res.data.data[i].lowBp)
                    that.data.datahighBp.push(res.data.data[i].highBp)
                    that.data.datahp.push(res.data.data[i].hp)
                    that.data.dataweight.push(res.data.data[i].weight)
                    that.data.datecreateTime.push(res.data.data[i].createTime)
                }
                console.log('that.data.datalowBp', that.data.datalowBp)
                console.log('that.data.datahighBp', that.data.datahighBp)
                console.log('that.data.datahp', that.data.datahp)

                chart.setOption({
                    xAxis: {
                        data: that.data.datecreateTime
                    },
                    series: [{
                        name: '重量',
                        data: that.data.dataweight
                    }]
                })

                chart1.setOption({
                    xAxis: {
                        data: that.data.datecreateTime
                    },
                    series: [{
                        name: '心跳',
                        data: that.data.datahp
                    }]
                })

                chart2.setOption({
                    xAxis: {
                        data: that.data.datecreateTime
                    },
                    series: [
                        {
                            name: '高压',
                            data: that.data.datahighBp
                        }, {
                            name: '低压',
                            data: that.data.datalowBp
                        }
                    ]
                })

            },
            fail: function (res) {
                console.log(res)
            }
        })


    },

    onLoad: function (options) {

        let that = this;
        wx.setStorageSync('startdate', this.data.startdate)
        wx.setStorageSync('enddate', this.data.enddate)
        wx.setStorageSync('type', this.data.type)

        let data = {
            "userId": 18721260590,
            "startDate": "",
            "endDate": "",
            "type": 0
        }
        wx.request({
            url: 'https://calamar.top:8081/measure/history',
            method: 'POST',
            data: data,    //参数为键值对字符串
            header: {
                //设置参数内容类型为x-www-form-urlencoded
                'content-type': 'application/json'
            },
            success: function (res) {
                that.setData({
                    startdate: res.data.data[0].startDate,
                    enddate: res.data.data[0].endDate,
                })
                console.log(res.data)
                that.setData({
                    List1: res.data.data
                })
            }
        })
    },

    onReady() {
    }
});

